{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d75bd182",
   "metadata": {},
   "source": [
    "### 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4b51773",
   "metadata": {},
   "source": [
    "列表：用于储存任意数目，任意类型的数据集合。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b30d85ee",
   "metadata": {},
   "source": [
    "### 列表的创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77137161",
   "metadata": {},
   "source": [
    "#### 1.基本语法[ ]创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5bdd745",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 'jack', True, 100]\n",
    "b = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b668724b",
   "metadata": {},
   "source": [
    "#### 2.list（）创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45a00c5b",
   "metadata": {},
   "source": [
    "使用list()可以将任何可迭代的数据转化成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fc01504e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = list()  # 创建一个空列表\n",
    "b = list(range(5))  # [0, 1, 2, 3, 4]\n",
    "c = list('nice')  # ['n', 'i', 'c', 'e']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "643f57c2",
   "metadata": {},
   "source": [
    "#### 3.通过range()创建整数列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6c184d2",
   "metadata": {},
   "source": [
    "range()可以帮助我们非常方便的创建整数列表，这在开发中及其有用。语法格式为：range([start,]end[,step])\n",
    "\n",
    "start参数：可选，表示起始数字。默认是0。\n",
    "\n",
    "end参数：必选，表示结尾数字。\n",
    "\n",
    "step参数：可选，表示步长，默认为1。\n",
    "\n",
    "python3中range()返回的是一个range对象，而不是列表。我们需要通过list()方法将其转换成列表对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5f6ba815",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = list(range(-3, 2, 1))  # [-3, -2, -1, 0, 1]\n",
    "b = list(range(2, -3, -1))  # [2, 1, 0, -1, -2] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00b795b1",
   "metadata": {},
   "source": [
    "#### 4.列表推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f80517db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 1), (0, 2), (1, 1), (1, 2)]\n"
     ]
    }
   ],
   "source": [
    "a = [i * 2 for i in range(5) if i % 2 == 0]  # [0, 4, 8]\n",
    "\n",
    "points = [(x, y) for x in range(0, 2) for y in range(1, 3)]\n",
    "print(points)  # [(0, 1), (0, 2), (1, 1), (1, 2)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92b0265c",
   "metadata": {},
   "source": [
    "### 列表元素的增加"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "331c1bf5",
   "metadata": {},
   "source": [
    "当列表增加和删除元素时，列表会自动进行内存管理，大大减少了程序员的负担。但这个特点涉及列表元素的大量移动，效率较低。除非必要，我们一般只在列表的尾部添加元素或删除元素，这会大大提高列表的操作效率。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95c9a131",
   "metadata": {},
   "source": [
    "- append（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea491145",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-5-44e338c54797>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-5-44e338c54797>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    >>>a = [20,40]\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    ">>>a = [20,40]\n",
    ">>>a.append(80)\n",
    ">>>a\n",
    "[20,40,80]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21b36479",
   "metadata": {},
   "source": [
    "- +运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47f9ac8b",
   "metadata": {},
   "source": [
    "并不是真正的尾部添加元素，而是创建新的列表对象；将原列表的元素和新列表的元素依次复制到新的列表对象中。这样，会涉及大量的复制操作，对于操作大量元素不建议使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eaad8210",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 1, 4, 4, 2]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a = [3, 1, 4]\n",
    ">>> b = [4, 2]\n",
    ">>> a + b\n",
    "[3, 1, 4, 4, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ac10a2f",
   "metadata": {},
   "source": [
    "- extand（）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1883b64",
   "metadata": {},
   "source": [
    "将目标列表的所有元素添加到本列表的尾部，属于原地操作，不创建新的列表对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "263efa71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 4, 6]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a = [3, 2]\n",
    ">>> a.extend([4, 6])\n",
    ">>> a\n",
    "[3, 2, 4, 6]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcff35e4",
   "metadata": {},
   "source": [
    "- insert（）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76925f0f",
   "metadata": {},
   "source": [
    "使用insert()方法可以将指定的元素插入到列表对象的任意指定位置。这样会让插入位置后面所有的元素进行移动，会影响处理速度。涉及大量元素时，尽量避免使用。类似发生这种移动的函数还有：remove()、pop()、del()，它们在删除非尾部元素时也会发生操作位置后面元素的移动。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e7c507a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 'jack', 5, 8]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a = [2, 5, 8]\n",
    ">>> a.insert(1, 'jack')\n",
    ">>> a\n",
    "[2, 'jack', 5, 8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74e74c2f",
   "metadata": {},
   "source": [
    "### 列表元素的删除"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "602fc410",
   "metadata": {},
   "source": [
    "- del()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "aaa2dea2",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-9-a322ac13fc74>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-9-a322ac13fc74>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    >>> a = [2, 3, 5, 7, 8]\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#删除列表指定位置的元素。\n",
    "\n",
    ">>> a = [2, 3, 5, 7, 8]\n",
    ">>> del a[1]\n",
    ">>> a\n",
    "[2, 5, 7, 8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffe13452",
   "metadata": {},
   "source": [
    "- pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d1e19c8e",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-10-b4a45053c104>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-10-b4a45053c104>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    >>> a = [3, 6, 7, 8, 2]\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#删除并返回指定位置元素，如果未指定位置则默认操作列表最后一个元素。\n",
    "\n",
    ">>> a = [3, 6, 7, 8, 2]\n",
    ">>> b = a.pop()\n",
    ">>> b\n",
    "2\n",
    ">>> c = a.pop(1)\n",
    ">>> c\n",
    "6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3406560",
   "metadata": {},
   "source": [
    "- remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "78524e55",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-12-e64dd5adf497>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-12-e64dd5adf497>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    >>> a=[10,20,30,40,50,20,30,20,30]\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#删除首次出现的指定元素，若不存在该元素抛出异常。\n",
    "\n",
    ">>> a=[10,20,30,40,50,20,30,20,30]\n",
    ">>> a.remove(20)\n",
    ">>> a\n",
    "[10, 30, 40, 50, 20, 30, 20, 30]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "623145c3",
   "metadata": {},
   "source": [
    "- clear()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a9f486c",
   "metadata": {},
   "source": [
    "清空一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bc7a68b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "a = [3,6,7,8,2]\n",
    "a.clear()\n",
    "print(a) # []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b7d43bf",
   "metadata": {},
   "source": [
    "### 列表元素的访问"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f04040a6",
   "metadata": {},
   "source": [
    "- 通过索引直接访问元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2a1ec8bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a = [2,4,6]\n",
    ">>> a[1]\n",
    "4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98760e1b",
   "metadata": {},
   "source": [
    "- index()获得指定元素在列表中首次出现的索引\n",
    "index()可以获取指定元素首次出现的索引位置。语法是：index(value,[start,[end]])。其中，start和end指定了搜索的范围。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "47e7c354",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a = [10, 20, 30, 40, 50, 20, 30, 20, 30]\n",
    ">>> a.index(20)\n",
    "1\n",
    ">>> a.index(20, 3)\n",
    "5\n",
    ">>> a.index(20, 6, 8)\n",
    "7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b43f10fc",
   "metadata": {},
   "source": [
    "### 列表元素出现的次数\n",
    "返回指定元素在列表中出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "de633f56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a = [10, 20, 30, 40, 50, 20, 30, 20, 30]\n",
    ">>> a.count(20)\n",
    "3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e538899",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
